/**
 * @description: 全员营销活动|添加或修改
 *
 * @author Yangcl
 * @date 2018年7月9日 下午21:00:52
 * @version 1.0.0.1
 */
var aemTask = {
		cid : null ,
		openId : null,
		staffId : null,
		aemTypeId : null,
		departmentId : null,
		path : null,

		type : null,

		ct : null , // 全部任务-点击事件-保存div对象
		aemUrl : null , // 活动静态页地址

		startIndex: 0,
		pageSize:10,

		init : function(cid_ , staffId_ , openId_ , path_ , departmentId_){
			aemTask.cid = cid_;
			aemTask.staffId = staffId_;
			aemTask.openId = openId_;
			aemTask.path = path_;
			aemTask.departmentId = departmentId_;
			return aemTask;
		},

		// 判断跳转来自哪里 web 管理后台|wechat 微信登录页
		comeFrom(type_){
			aemTask.type = type_;
			return aemTask;
		},

		// 	绘制全部任务
		drawAllTask : function(o){
			$("#aem-type-list").show();
			$(".mint-tab-item").removeClass("is-selected");
			$("#all-task-tag").addClass("is-selected");
			$(".mint-tab-container-item").hide();
			$("#div-all-task").show();
			$("#all-task").children().remove();
			$("#aem-type-list").children().remove();
			if(typeof(o) == 'object'){
				aemTask.aemTypeId = $(o).attr("typeId");
			}else{
				aemTask.aemTypeId = null;
			}

			var data = {
					cid : aemTask.cid,
					staffId : aemTask.staffId,
					aemTypeId : aemTask.aemTypeId,
					departmentId : aemTask.departmentId
			};
			var url = aemTask.getPath() +"/aem/client/ajaxClientAemInfoList";
			var e = JSON.parse(aemTask.syncAjax("post", url , data , false));
			if(e.status == "200"){
				var arr = e.list;
				var html_ = '';
				var onclick_ = 'onclick="aemTask.checkTask(this);" ';
				if(aemTask.type == 'web'){
					onclick_ = ' '; // 来自管理后台的跳转请求则过滤掉onclick事件响应
				}
				for(var i = 0 ; i < arr.length ; i ++){
					html_ += '<div data-v-580431b3="" class="activity-thumb" attr-join="' + arr[i].isJoin + '" ' + onclick_ + ' attr-url="'
										+ arr[i].url + '" attr-cid="' + arr[i].cid + '" attr-id="' + arr[i].id + '" attr-level="'
										+ 1 + '" attr-mgiId="' + arr[i].mgiId + '" attr-click="' + arr[i].clickSore + '" attr-share="' + arr[i].shareScore + '" attr-asrId="' + arr[i].asrId + '">'
									+ '<img data-v-580431b3="" class="thumb" src="' + arr[i].pic + '">'
									+ '<p data-v-580431b3="" class="title">' + arr[i].name + '</p>'
									+ '<p data-v-580431b3="" class="time">任务时间：' + arr[i].startTime.substring(0 , arr[i].startTime.length - 5) + ' 至 ' + arr[i].endTime.substring(0 , arr[i].endTime.length - 5) + '</p>'
									+ '<p data-v-580431b3="" class="' + arr[i].isJoin + '">' + arr[i].join +  '</p>'
								+ '</div>';
				}
				$("#all-task").append(html_);
			}else{
				$("#all-task").append('<div class="more" style="margin-bottom:40px">暂无数据</div>');
			}

			var typeId = null;
			if(typeof(o) == 'object'){
				typeId = $(o).attr("typeId");
			}
			var html_ = '';
			var atl = e.aemType;
			for(var i = 0 ; i < atl.length ; i ++){
				if(typeId == atl[i].id){
					html_ += '<span typeId="' + atl[i].id + '" onclick="aemTask.drawAllTask(this)" class="active">' + atl[i].name + '</span>'
				}else{
					html_ += '<span typeId="' + atl[i].id + '" onclick="aemTask.drawAllTask(this)">' + atl[i].name + '</span>'
				}
			}
			$("#aem-type-list").append(html_);
		},

		// 绘制我的任务
		drawMyTask : function(o){
			$("#aem-type-list").hide();
			$(".mint-tab-item").removeClass("is-selected");
			$("#my-task-tag").addClass("is-selected");
			$(".mint-tab-container-item").hide();
			$("#div-my-task").show();
			// $("#my-task-list").children().remove();
			$("#next-page").remove();

			var html_ = '';
			if(o == "tag" && $("#div-my-task").find(".Mission-item").length != 0){
				html_ += '<div id="next-page" onclick="aemTask.drawMyTask(\'bottom\')" class="more" style="margin-bottom:40px">暂无数据</div>';
				$("#my-task-list").append(html_);
				return;
			}

			var data = {
					cid : aemTask.cid,
					staffId : aemTask.staffId,
					startIndex : aemTask.startIndex,
					pageSize : aemTask.pageSize,
			};
			var url = aemTask.getPath() +"/aem/client/ajaxClientMyAemInfoList";
			var e = JSON.parse(aemTask.syncAjax("post", url , data , false));
			if(e.status == "200" && e.list.length > 0){
				var arr = e.list;
				for(var i = 0 ; i < arr.length ; i ++){
					if(arr.length == 1 && $("#mi-" + arr[i].id).length == 1){
						html_ += '<div id="next-page" onclick="aemTask.drawMyTask(\'bottom\')" class="more" style="margin-bottom:40px">暂无数据</div>';
						$("#my-task-list").append(html_);
						return;
					}
					html_ += '<div class="Mission-item" id="mi-' + arr[i].id + '">'
									+ '<div class="Mission-tt">' + arr[i].name + '</div>'
									+ '<div class="Mission-time">任务时间：' + arr[i].startTime + ' 至 ' + arr[i].endTime + '</div>'
									+ '<div class="mission-scan">'
										+ '<img src="' + arr[i].pic + '" attr-url="' + arr[i].url + '" onclick="aemTask.aemH5Page(this)">'
										+ '<div class="mission-tt">参与人数: ' + arr[i].aemUv + '</div>'
									+ '</div>'
									+ '<div class="time-bg">'
										+ '<div class="time">参与时间：' + arr[i].createTime + '</div>'
										+ '<div class="transmit-effect">'
											+ '传播效果：转发<span class="number">' + arr[i].shareUv + '</span>点击<span>' + arr[i].uv	 + '</span>'
										+ '</div>'
									+ '</div>'
									+ '<div class="transmit-bg">'
										+ '<ul class="transmit-item clearfix">'
											+ '<li class="right-line" aemId="' + arr[i].aemId + '" aemName="' + arr[i].name + '" onclick="aemTask.aemStatics(this)">'
												+ '<div class="transmit-icon">'
													+ '<img src="">'
													+ '统计'
												+ '</div>'
											+ '</li>'
											+ '<li aemId="' + arr[i].aemId + '" onclick="aemTask.aemSpread(this)">'
												+ '<div class="transmit-icon">'
													+ '<img src="">'
													+ '传播效果'
												+ '</div>'
											+ '</li>'
										+ '</ul>'
									+ '</div>'
								+ '</div>' ;
				}
				html_ += '<div id="next-page" onclick="aemTask.myTaskNextPage()" class="more" style="margin-bottom:40px">查看更多</div>';
				$("#my-task-list").append(html_);
			}else{
				html_ += '<div id="next-page" onclick="aemTask.drawMyTask(\'bottom\')" class="more" style="margin-bottom:40px">暂无数据</div>';
				$("#my-task-list").append(html_);
				return;
			}
		},


		// 我的任务 - 查看更多
		myTaskNextPage : function(){
			aemTask.startIndex = aemTask.startIndex + 10;
			aemTask.drawMyTask('bottom');
		},


		/**
		 *	http://smzy.cecook.cn/h5/1/1016209646073024512.html?
		 *		cid=1&
		 *		aemId=1016209646073024512&
		 *		level=1&
		 *		mgiId=1016207679225466880&
		 *		openId=oUclx0oaUbx7yqnPsoTb1EyL7qEY&
		 *		parentOpenId=null&
		 *		staffId=1016240335803781120
		 *
		 */
		checkTask : function(o){
			var url = $(o).attr("attr-url") + "?cid=" + $(o).attr("attr-cid") + "&aemId=" + $(o).attr("attr-id") + "&level=" + $(o).attr("attr-level") + "&mgiId="  + $(o).attr("attr-mgiId")
								+"&openId=" + aemTask.openId +"&parentOpenId=null&staffId=" + aemTask.staffId + "&departmentId=" + aemTask.departmentId;
			if($(o).attr("attr-join") == "has-join"){
				window.location.href = url + "&asrId=" + $(o).attr("attr-asrId");
			}else{
				$(".gray").show();
				$("#aem-join-dialog").show();
				$("#span-share-score").text($(o).attr("attr-share"));
				$("#span-click-score").text($(o).attr("attr-click"));
				aemTask.ct = o;
				aemTask.aemUrl = url;
			}
		},

		// 弹窗关闭
		dialogClose : function(){
			$(".gray").hide();
			aemTask.ct = null;
			aemTask.aemUrl = null;
		},

		// 接受任务
		acceptTask : function(){
			var o = aemTask.ct;
			var data={
					cid : $(o).attr("attr-cid"),
					aemId : $(o).attr("attr-id"),
					level : $(o).attr("attr-level"),
					mgiId : $(o).attr("attr-mgiId"),
					openId : aemTask.openId,
					userIdentity:3,								// 用户身份 3.员工
					staffId : aemTask.staffId,
					spreadType:1
			}
			var url = aemTask.getPath() +"/aem/client/ajaxClientJoinAemTask";
			var e = JSON.parse(aemTask.syncAjax("post", url , data , false));
			if(e.status == "200"){
				window.location.href = aemTask.aemUrl + "&asrId=" + e.asr.id;
			}
		},

		// 我的任务 - 点击图片跳转到h5静态页
		aemH5Page : function(o){
			window.location.href = $(o).attr("attr-url") + "&departmentId=" + aemTask.departmentId;
		},

		// 统计
		aemStatics : function(o){
			window.location.href = aemTask.getPath() + "/aem/client/aemDistStaticsView?aemId=" + $(o).attr("aemId") + "&aemName=" + encodeURI(encodeURI($(o).attr("aemName"))) + "&cid=" + aemTask.cid;
		},

		// 传播效果
		aemSpread : function(o){
			window.location.href = aemTask.getPath() + "/aem/client/aemDiskTaskView?aemId=" + $(o).attr("aemId") + "&cid=" + aemTask.cid;
		},


		// aemTask.getPath()
		getPath : function(){
			return aemTask.path;
		},

		trim : function(str) {
		    return str.replace(/(^\s+$)/g, "");
		},

		/**
		 * 发送ajax请求
		 *
		 * type_ : post or get
		 * url_ : url
		 * data_ : data_
		 * async_ : sync type ,true or false
		 */
		syncAjax : function(type_, url_, data_  , async_) {
			var msg_ = null;
			$.ajax({
					dataType : "text",
					type : type_,
					url : url_,
					data : data_,
					async : async_,
					success : function(msg) {
							msg_ = msg;
					},
					error : function(msg) {
							msg_ = msg;
					}
			});
			return msg_;
		}
}



















